package com.ciwong.epaper.modules.epaper.dao;

import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.dbbean.LswAnswer;
import com.ciwong.epaper.modules.dbbean.LswProgress;
import com.ciwong.mobilelib.i.BaseCallBack;
import com.ciwong.mobilelib.utils.ThreadTask;
import com.google.gson.Gson;

/**
 * Created by Administrator on 2017/2/16.
 * 听说模考专用
 */
public class LswDao {

    private static LswDao instance;
    private Handler mHandler;

    public LswDao() {
        mHandler = new Handler(Looper.getMainLooper());
    }

    /**
     * 获取唯一实例
     *
     * @return 唯一实例
     */
    public static LswDao getInstance() {
        if (instance == null) {
            syncInit();
        }
        return instance;
    }

    /**
     * 实例化对象
     */
    private static synchronized void syncInit() {
        if (instance == null) {
            instance = new LswDao();
        }
    }

    /**
     * 提交mp3 碎片打分
     */
    public void submitMakeScore(LswAnswer lswAnswer, final BaseCallBack callBack) {

        LswRequest.submitMakeScore(lswAnswer, callBack);

    }

    /**
     * 设置做作业进度
     */
    public void onUploadDoworkProgress(final LswProgress lswProgress, final BaseCallBack callBack) {

        ThreadTask.getInstance().executorDBThread(new Runnable() {
            @Override
            public void run() {
                String json = new Gson().toJson(lswProgress, LswProgress.class);
                try {
                    LswRequest.onUploadDoworkProgress(json, callBack);
                } catch (Exception e) {
                    if (e != null) {
                        Log.d("onUploadDoworkProgress", "############e#############" + e.getMessage());
                    }
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            callBack.failed(R.string.unknown_error);
                        }
                    });
                    return;
                }


            }
        }, ThreadTask.ThreadPeriod.PERIOD_HIGHT);

    }

}
